package com.zjpg.forum.services.impl;

import com.zjpg.forum.common.AppResult;
import com.zjpg.forum.common.ResultCode;
import com.zjpg.forum.dao.ThumbListMapper;
import com.zjpg.forum.exception.ApplicationException;
import com.zjpg.forum.model.ThumbList;
import com.zjpg.forum.services.IThumbListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class ThumbListServiceImpl implements IThumbListService {
    @Resource
    private ThumbListMapper thumbListMapper;

    @Override
    public int insertSelective(ThumbList thumbList) {
        return thumbListMapper.insertSelective(thumbList);
    }

    @Override
    public ThumbList selectByPrimaryKey(Long articleId, Long thumbUserId) {
        return thumbListMapper.selectByPrimaryKey(articleId,thumbUserId);
    }

    @Override
    public boolean updateByPrimaryKeySelective(ThumbList thumbList) {
        if (thumbList == null || thumbList.getId() <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        ThumbList updateThumbList = new ThumbList();
        updateThumbList.setId(thumbList.getId());
        updateThumbList.setState(1L);
        int row = thumbListMapper.updateByPrimaryKeySelective(updateThumbList);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        log.info("点赞成功");
        return true;
    }
}
